package com.example.prison.util;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    /**
     * 将当前时间减去hour小时并返回 yyyy-MM-dd HH:mm:ss
     */
    public static String hourProcess(Integer hour) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.HOUR,hour);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(dt1);
    }

    /**
     * 将当前时间减去min分钟并返回 yyyy-MM-dd HH:mm:ss
     */
    public static String minProcess(Integer min) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.MINUTE,min);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(dt1);
    }

    /**
     * 从指定的时间字符串(yyyy-MM-dd HH:mm:ss)中减去特定的分钟数
     * @param dateTimeString  2024-07-08 15:30:00
     * @param minutesToSubtract
     * @return
     */
    public static String minReduce(String dateTimeString, Integer minutesToSubtract) {
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 解析字符串为 LocalDateTime 对象
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
        // 减去分钟
        LocalDateTime newDateTime = dateTime.minusMinutes(minutesToSubtract);
        // 格式化新的日期时间为字符串
        String newDateTimeString = newDateTime.format(formatter);
        return newDateTimeString;
    }


}
